home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 001a / mskrmsrc.zip / MSNTCP.H < prev    next >
C/C++ Source or Header  |  1991-10-24  |  15KB  |  463 lines

  1. /* File MSNTCP.H
  2.  * Main include file for TCP/IP, as revised and modified for MS-DOS Kermit.
  3.  *
  4.  * Copyright (C) 1991, University of Waterloo.
  5.  * Copyright (C) 1991, Trustees of Columbia University in the
  6.  *  City of New York.  Permission is granted to any individual or
  7.  *  institution to use, copy, or redistribute this software as long as
  8.  *  it is not sold for profit and this copyright notice is retained.
  9.  *
  10.  * Original version created by Erick Engelke of the University of
  11.  *  Waterloo, Waterloo, Ontario, Canada.
  12.  *       Erick Engelke                Erick@development.watstar.uwaterloo.ca
  13.  *       Faculty of Engineering
  14.  *       University of Waterloo       (519) 885-1211 Ext. 2965
  15.  *       200 University Ave.,
  16.  *       Waterloo, Ont., Canada
  17.  *       N2L 3G1
  18.  * Adapted and modified for MS-DOS Kermit by Joe R. Doupnik, 
  19.  *  Utah State University, jrd@cc.usu.edu, jrd@usu.Bitnet,
  20.  *  and by Frank da Cruz, Columbia University, fdc@watsun.cc.columbia.edu.
  21.  *
  22.  * Name resolution services were adapted from sources made available by
  23.  * the National Centre for Supercomputer Applications (NCSA) and Clarkson
  24.  * University.
  25.  *
  26.  * The C code is designed for the small memory model of Microsoft C versions
  27.  * 5.1 and 6.00A, with structures packed on one byte boundaries. No other
  28.  * options are expected.
  29.  *
  30.  * Last edit:
  31.  * 6 Sept 1991
  32.  */
  33.  
  34. /* Kernel version (major major minor minor) */
  35. #define WTCP_VER 0x0311
  36. #define KERMIT
  37.  
  38. /*
  39.  * Typedefs and constants
  40.  */
  41.  
  42. #ifndef byte
  43. typedef unsigned char byte;
  44. #endif  /* byte */
  45. #ifndef word
  46. typedef unsigned int word;
  47. #endif  /* word */
  48. #ifndef longword
  49. typedef unsigned long longword;
  50. #endif /* longword */
  51.  
  52. /*
  53. #define DEBUG
  54. */
  55.  
  56. #ifndef NULL
  57. #define NULL 0
  58. #endif
  59.  
  60. #define TRUE        1
  61. #define FALSE       0
  62.  
  63. #ifdef KERMIT
  64. #define ETH_MSS 536             /* MSS for Ethernet */
  65. #define tcp_MaxBufSize 1024    /* max bytes to buffer in a tcp socket */
  66. #define udp_MaxBufSize 534    /* max bytes to buffer in a udp socket */
  67. #define MAX_GATE_DATA 2
  68. #define MAX_NAMESERVERS 3
  69. #define MAX_COOKIES 0
  70. #else
  71. #define ETH_MSS 1500            /* MSS for Ethernet */
  72. #define tcp_MaxBufSize 2048    /* maximum bytes to buffer in a socket */
  73. #define udp_MaxBufSize 2048    /* max bytes to buffer in a udp socket */
  74. #define MAX_GATE_DATA 12
  75. #define MAX_NAMESERVERS 10
  76. #define MAX_COOKIES 10
  77. #endif
  78.  
  79. #define MAX_STRING 50    
  80. #define TICKS_SEC 18
  81. #define PD_ETHER 1
  82. #define PD_SLIP  6
  83.  
  84. /* These are Ethernet protocol numbers but are used for other things too */
  85. #define UDP_PROTO  0x11
  86. #define TCP_PROTO  0x06
  87. #define ICMP_PROTO 0x01
  88.  
  89. #define TCP_MODE_BINARY  0       /* default mode */
  90. #define TCP_MODE_ASCII   1
  91. #define TCP_MODE_NAGLE   0       /* Nagle algorithm */
  92. #define TCP_MODE_NONAGLE 4
  93. #define UDP_MODE_CHK     0       /* default to having checksums */
  94. #define UDP_MODE_NOCHK   2       /* turn off checksums */
  95.  
  96. typedef int (*procref)();
  97. typedef byte eth_address[6];
  98.  
  99. /* The Ethernet header */
  100. typedef struct {
  101.     eth_address     destination;
  102.     eth_address     source;
  103.     word            type;
  104. } eth_Header;
  105.  
  106. /* The Internet Header: */
  107. typedef struct {
  108.     byte        hdrlen_ver;        /* both in one byte */
  109.     byte        tos;
  110.     word            length;
  111.     word            identification;
  112.     word            frag;
  113.     byte        ttl;
  114.     byte        proto;
  115.     word            checksum;
  116.     longword        source;
  117.     longword        destination;
  118. } in_Header;
  119.  
  120.  
  121. #define in_GetVersion(ip) ((ip)->hdrlen_ver >> 4)
  122. #define in_GetHdrlen(ip)  ((ip)->hdrlen_ver & 0xf)  /* 32 bit word size */
  123. #define in_GetHdrlenBytes(ip)  (in_GetHdrlen(ip) << 2) /* 8 bit byte size */
  124. #define in_GetTos(ip)      ((ip)->tos)
  125. #define in_GetTTL(ip)      ((ip)->ttl)
  126. #define in_GetProtocol(ip) ((ip)->proto)
  127.  
  128. typedef struct {
  129.     word        srcPort;
  130.     word        dstPort;
  131.     word        length;
  132.     word        checksum;
  133. } udp_Header;
  134.  
  135. #define UDP_LENGTH (sizeof(udp_Header))
  136.  
  137. typedef struct {
  138.     word            srcPort;
  139.     word            dstPort;
  140.     longword        seqnum;
  141.     longword        acknum;
  142.     word            flags;
  143.     word            window;
  144.     word            checksum;
  145.     word            urgentPointer;
  146. } tcp_Header;
  147.  
  148. #define tcp_FlagFIN     0x0001
  149. #define tcp_FlagSYN     0x0002
  150. #define tcp_FlagRST     0x0004
  151. #define tcp_FlagPUSH    0x0008
  152. #define tcp_FlagACK     0x0010
  153. #define tcp_FlagURG     0x0020
  154. #define tcp_FlagDO      0xF000
  155. #define tcp_GetDataOffset(tp) (intel16((tp)->flags) >> 12)
  156.  
  157. /* The TCP/UDP Pseudo Header */
  158. typedef struct {
  159.     longword    src;
  160.     longword    dst;
  161.     byte        mbz;
  162.     byte        protocol;
  163.     word        length;
  164.     word        checksum;
  165. } tcp_PseudoHeader;
  166.  
  167. /*
  168.  * TCP states, from tcp manual.
  169.  * Note: close-wait state is bypassed by automatically closing a connection
  170.  *       when a FIN is received.  This is easy to undo.
  171.  */
  172. #define tcp_StateLISTEN  0      /* listening for connection */
  173. #define tcp_StateSYNSENT 1      /* syn sent, active open */
  174. #define tcp_StateSYNREC  2      /* syn received, synack+syn sent. */
  175. #define tcp_StateESTAB   3      /* established */
  176. #define tcp_StateFINWT1  4      /* sent FIN */
  177. #define tcp_StateFINWT2  5      /* sent FIN, received FINACK */
  178. #define tcp_StateCLOSWT  6      /* received FIN waiting for close */
  179. #define tcp_StateCLOSING 7      /* sent FIN, received FIN (waiting for FINACK) */
  180. #define tcp_StateLASTACK 8      /* fin received, finack+fin sent */
  181. #define tcp_StateTIMEWT  9      /* dally after sending final FINACK */
  182. #define tcp_StateCLOSEMSL 10
  183. #define tcp_StateCLOSED  11     /* finack received */
  184.  
  185. /*
  186.  * UDP socket definition
  187.  */
  188. typedef struct udp_socket {
  189.     struct udp_socket *next;
  190.     word        ip_type;        /* always set to UDP_PROTO */
  191.     byte       *err_msg;        /* null when all is ok */
  192.     void      (*usr_yield)();
  193.     word        soc_mode;            /* a logical OR of bits */
  194.     longword        usertimer;        /* ip_timer_set, ip_timer_timeout */
  195.     procref        dataHandler;
  196.     eth_address     hisethaddr;        /* peer's ethernet address */
  197.     longword        hisaddr;        /* peer's internet address */
  198.     word        hisport;        /* peer's UDP port */
  199.     word        myport;
  200.     int             rdatalen;           /* must be signed */
  201.     byte        rdata[udp_MaxBufSize];
  202. } udp_Socket;
  203.  
  204. /*
  205.  * TCP Socket definition
  206.  */
  207.  
  208. typedef struct tcp_socket {
  209.     struct tcp_socket *next;
  210.     word        ip_type;        /* always set to TCP_PROTO */
  211.     byte        *err_msg;
  212.     void      (*usr_yield)();
  213.     word        soc_mode;        /* a logical OR of bits */
  214.  
  215.     longword        usertimer;        /* ip_timer_set, ip_timer_timeout */
  216.     procref         dataHandler;    /* called with incoming data */
  217.     eth_address     hisethaddr;     /* ethernet address of peer */
  218.     longword        hisaddr;        /* internet address of peer */
  219.     word            hisport;        /* tcp ports for this connection */
  220.     word        myport;
  221.  
  222.     int             rdatalen;       /* must be signed */
  223.     byte        rdata[tcp_MaxBufSize];    /* received data */
  224.     word        rmaxdatalen;    /* normally tcp_MaxBufSize */
  225.     word        state;          /* connection state */
  226.  
  227.     longword        acknum;
  228.     longword        seqnum;         /* data ack'd and sequence num */
  229.     long            timeout;        /* timeout, in milliseconds */
  230.     byte            unhappy;        /* flag, indicates retransmitting segt's */
  231.     word            flags;          /* tcp flags word for last packet sent */
  232.  
  233.     word        window;        /* other guy's window */
  234.     int         datalen;        /* number of bytes of data to send */
  235.                     /* must be signed */
  236.  
  237.     byte        cwindow;        /* Van Jacobson's algorithm */
  238.     byte        wwindow;
  239.  
  240.     word        vj_sa;        /* VJ's alg, standard average */
  241.     word        vj_sd;        /* VJ's alg, standard deviation */
  242.     longword        vj_last;        /* last transmit time */
  243.     word        rto;
  244.     byte        karn_count;        /* count of packets */
  245.  
  246.     /* retransmission timeout proceedure, these are in PC clock ticks */
  247.     longword        rtt_lasttran;       /* last transmission time */
  248.     longword        rtt_smooth;         /* smoothed round trip time */
  249.     longword        rtt_delay;          /* delay for next transmission */
  250.     longword        rtt_time;           /* time of next transmission */
  251.     word            mss;
  252.     byte            data[tcp_MaxBufSize];     /* data to send */
  253. } tcp_Socket;
  254.  
  255. /* sock_type used for socket io */
  256. typedef union {
  257.     udp_Socket udp;
  258.     tcp_Socket tcp;
  259. } sock_type;
  260.  
  261. /* similar to UNIX */
  262. typedef struct sockaddr {
  263.     word        s_type;
  264.     word        s_port;
  265.     longword    s_ip;
  266.     byte        s_spares[6];    /* unused in TCP realm */
  267. };
  268.  
  269. /*
  270.  * ARP definitions
  271.  */
  272. #define arp_TypeEther  0x100        /* ARP type of Ethernet address */
  273.  
  274. /* ARP style op codes */
  275. #define ARP_REQUEST 0x0100
  276. #define ARP_REPLY   0x0200
  277. #define RARP_REQUEST 0x0300        /* RARP request */
  278. #define RARP_REPLY  0x0400        /* RARP reply */
  279.  
  280. /* Arp header */
  281. typedef struct {
  282.     word            hwType;
  283.     word            protType;
  284.     word            hwProtAddrLen;  /* hw and prot addr len */
  285.     word            opcode;
  286.     eth_address     srcEthAddr;
  287.     longword        srcIPAddr;
  288.     eth_address     dstEthAddr;
  289.     longword        dstIPAddr;
  290. } arp_Header;
  291.  
  292. /*
  293.  * socket macros
  294.  */
  295.  
  296. /*
  297.  * sock_wait_established()
  298.  *    - waits then aborts if timeout on s connection
  299.  * sock_wait_input()
  300.  *    - waits for received input on s
  301.  *    - may not be valid input for sock_Gets... check returned length
  302.  * sock_tick()
  303.  *    - do tick and jump on abort
  304.  * sock_wait_closed();
  305.  *    - discards all received data
  306.  *
  307.  * jump to sock_err with contents of *statusptr set to
  308.  *     1 on closed
  309.  *    -1 on timeout
  310.  *
  311.  */
  312. #define sock_wait_established(s, seconds, fn, statusptr) \
  313.     if (ip_delay0(s, seconds, fn, statusptr)) goto sock_err;
  314. #define sock_wait_input(s, seconds, fn , statusptr) \
  315.     if (ip_delay1(s, seconds, fn, statusptr)) goto sock_err;
  316. #define sock_tick(s, statusptr) \
  317.     if (!tcp_tick(s)) { *statusptr = 1 ; goto sock_err; }
  318. #define sock_wait_closed(s, seconds, fn, statusptr)\
  319.     if (ip_delay2(s, seconds, fn, statusptr)) goto sock_err;
  320.  
  321. longword resolve();
  322. int    ping(longword host, longword countnum);
  323. longword chk_ping(longword host, longword *ptr);
  324. int    add_server(int *counter, int max, longword *array, longword value);
  325. byte *    rip(byte *s);
  326. chk_socket(tcp_Socket *s);
  327. byte *    inet_ntoa(byte *s, longword x);
  328. byte *    psocket(tcp_Socket *s);
  329. longword inet_addr(byte *s);
  330. byte *    sockerr(tcp_Socket *s);
  331. byte *    sockstate(tcp_Socket *s);
  332. byte *    getdomainname(byte *name, int length);
  333. byte *    setdomainname(byte *string);
  334.  
  335. sock_init(void);
  336. /* s is a pointer to a udp or tcp socket */
  337. sock_read(void *s, byte *dp, int len);
  338. sock_fastread(void *s, byte *dp, int len);
  339. sock_write(void *s, byte *dp, int len);
  340. sock_fastwrite(void *s, byte *dp, int len);
  341. sock_flush(void *s);
  342. sock_flushnext(void *s);
  343. sock_puts(void *s, byte *dp);
  344. word sock_gets(void *s, byte *dp, int n);
  345. sock_putc(void *s, byte c);
  346. sock_getc(void *s);
  347. word sock_dataready(void *s);
  348. sock_close(void *s);
  349. void sock_abort(void *s);
  350. sock_printf(void *s, byte *format, ...);
  351. sock_scanf(void *s, byte *format, ...);
  352. sock_mode(void *s, word mode);    /* see TCP_MODE_... */
  353. db_write(byte *msg);
  354. dbuginit();
  355.  
  356. /*
  357.  * TCP or UDP specific material, must be used for open's and listens, but
  358.  * sock calls are used for everything else.
  359.  */
  360. int    udp_open(void *s, word lport, longword ina, word port,
  361.         int (*datahandler)());
  362. int    tcp_open(void *s, word lport, longword ina, word port,
  363.         int (*datahandler)());
  364. int    tcp_listen(void *s, word lport, longword ina, word port,
  365.         int (*datahandler)(), word timeout);
  366. int    tcp_established(void *s);
  367.  
  368. /* less general functions */
  369. byte *    rip(byte *s);
  370. longword resolve(byte *name);
  371. int    isaddr(byte *text);
  372. longword intel(longword x);
  373. word    intel16(word x);
  374.  
  375. /* timers */
  376. void    ip_timer_init(void * s, int delayseconds);
  377. int    ip_timer_expired(void * s);
  378. /* tcp_init/tcp_shutdown, init/kill all tcp and lower services.
  379.    Call if sock_init is not used, else not recommended.
  380. */
  381. int    tcp_Init();
  382. void    tcp_shutdown();
  383. int    tcp_abort(tcp_Socket *s);
  384. /* tcp_tick - called periodically by user application in sock_wait.
  385.   returns 0 when our socket closes
  386. */
  387. int    tcp_tick(void *s);
  388. /* tcp_set_debug_state - set 1 or reset 0, development tool */
  389. void    tcp_set_debug_state(word x);
  390.  
  391. int    tcp_cancel(void * ip);
  392. int    udp_cancel(void * ip);
  393.  
  394. longword aton(byte * string);
  395. int    ping(longword host, longword countnum);
  396. longword chk_ping(longword host, longword *ptr);
  397. void    arp_register(longword use, longword instead_of);
  398.  
  399. int    eth_init();
  400. byte *    eth_formatpacket(void *eth_address, word eth_type);
  401. int    eth_send(word len);
  402. void    eth_free(void *buf);
  403. byte *    th_arrived(word *type_ptr);
  404. void    eth_release();
  405. void *    eth_hardware(void * p);
  406.  
  407. /* bsd-similar stuff */
  408.  
  409. int    sock_rbsize(void *s);
  410. int    sock_rbused(void *s);
  411. int    sock_rbleft(void *s);
  412. int    sock_tbsize(void *s);
  413. int    sock_tbused(void *s);
  414. int    sock_tbleft(void *s);
  415. getpeername(tcp_Socket *s, void *dest, int *len);
  416. getsockname(tcp_Socket *s, void *dest, int *len);
  417. longword gethostid();
  418. longword sethostid(longword ip);
  419.  
  420. chk_socket(tcp_Socket *s);
  421. byte *    inet_ntoa(byte *s, longword x);
  422. byte *    psocket(tcp_Socket *s);
  423. longword inet_addr(byte *s);
  424. byte *    sockerr(tcp_Socket *s);
  425. byte *    sockstate(tcp_Socket *s);
  426. getpeername(tcp_Socket *s, void *dest, int *len);
  427. getsockname(tcp_Socket *s, void *dest, int *len);
  428. longword gethostid();
  429. longword sethostid(longword ip);
  430. byte *    getdomainname(byte *name, int length);
  431. byte *    setdomainname(byte *string);
  432. byte *    gethostname(byte *name, int length);
  433. byte *    sethostname(byte *string);
  434. word     ntohs(word a);
  435. word     htons(word a);
  436. longword ntohl(longword x);
  437. longword htonl(longword x);
  438. longword realclock(void);
  439. void *    movmem(void *, void *, int);
  440.  
  441. void    arp_register(longword use, longword instead_of);
  442. int    arp_resolve(longword ina, void *ethap);
  443. void    arp_init(void);
  444.  
  445. extern    survivebootp;
  446. extern    word pktdevclass;
  447. extern    word mss;
  448. extern    word bootptimeout;
  449. extern    longword bootphost;
  450. extern    word bootpon;
  451. extern    longword my_ip_addr;
  452. extern    eth_address eth_addr;
  453. extern    eth_address eth_brdcast;
  454. extern    longword sin_mask;
  455. extern    word sock_delay;
  456. /*extern byte *def_domain;*/
  457. extern    int last_nameserver;
  458. extern    word debug_on;
  459. extern    longword def_nameservers[MAX_NAMESERVERS];
  460. extern    byte *hostname;
  461. /* user initialization file */
  462. extern    void (*usr_init)();
  463.